#ifndef MANAGER_H
#define MANAGER_H

#include "managercommon.h"

#include <QtCore/QMap>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/QObject>
#include <QtCore/QUuid>


template<class Item>
class Manager
{
public:
    typedef QList<Item *> Items;
    typedef QMap<long, Item *> ItemFinder;
    typedef QMap<long, int> IndexFinder;
    typedef typename ItemFinder::Iterator ItemFinderIterator;

public:
    Item *byIndex(int index);
    Item *byId(long id);

    int indexOf(Item *item);

    int count();

    const Items &items();

    void appendItem(Item *item);

    void removeItem(Item *item);

protected:
    Manager();
    virtual ~Manager();

protected:
    inline QMutex &mutex() {
        return m_mutex;
    }

    virtual void itemAboutToBeAdded(Item *item) {
        Q_UNUSED(item)
    }

    virtual void itemAdded(Item *item) {
        Q_UNUSED(item)
    }

    virtual void itemAboutToBeRemoved(Item *item) {
        Q_UNUSED(item)
    }

    virtual void itemRemoved(Item *item) {
        Q_UNUSED(item)
    }

private:
    QMutex m_mutex;

    Items m_items;
    ItemFinder m_itemFinder;
    IndexFinder m_indexFinder;
};

#endif // MANAGER_H
